// pages/change-user/change-user.js
var request = require('../../utils/request.js');
var config = require('../../config.js');
let hyMath = require('../../utils/math.js');

Page({
    /**
     * 页面的初始数据
     */
    data: {
      haveMemberData: true, //有会员
      noInternet: false,    //请求错误
        default_user_img: '../../images/ic-touxiang-moren.png',
        scroll:{
            height:0
        },
        searchP:{
            page_size:10,
            p:1,
            keyword :'',
            isOver : false
        },
        searchImg: '../../images/ic-search-black.png',
        list: [
            // {
            //     head_img_url:'',
            //     "real_name": "任永康改1", // 姓名
            //     "mobile": "17703592592", //  电话
            //     "member_id": "5a40a155e8fd7e625d230096", //会员id
            //     "old_member_id": "",  // 会员id
            //     "cars": [   //车辆列表
            //         {
            //             "id": "5a3cd45be8fd7e300b3aed6e",
            //             "license_number": "京GF2222"   //车牌号
            //         },
            //         {
            //             "id": "5a40a155e8fd7e625d230095",
            //             "license_number": "京123456"   //车牌号
            //         }
            //     ],
            //     "man_hour_coin": 0,//工时
            //     "synthesize_coin": 0, //综合
            //     "bonus": 300,  //积分
            //     "balance": 19.99,  //储值
            //     "level": "白金卡",   // 金卡5着那里
            // },
            // {
            //     head_img_url:'',
            //     "real_name": "任永康改1", // 姓名
            //     "mobile": "17703592592", //  电话
            //     "member_id": "5a40a155e8fd7e625d230096", //会员id
            //     "old_member_id": "",  // 会员id
            //     "cars": [   //车辆列表
            //         {
            //             "id": "5a3cd45be8fd7e300b3aed6e",
            //             "license_number": "京GF2222"   //车牌号
            //         },
            //         {
            //             "id": "5a40a155e8fd7e625d230095",
            //             "license_number": "京123456"   //车牌号
            //         }
            //     ],
            //     "man_hour_coin": 0,//工时
            //     "synthesize_coin": 0, //综合
            //     "bonus": 300,  //积分
            //     "balance": 19.99,  //储值
            //     "level": "白金卡",   // 金卡5着那里
            // }
        ]
    },
    tapSelectUser(e){
        let items = e.currentTarget.dataset.items
        // console.log('userItems')
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
            urlP: {items:items}
        })
        wx.navigateBack();
    },
    // 获取高度和赋值
    autoWindowHeight(){
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    'scroll.height': res.windowHeight
                })
            }
        })
    },
    inputKey: function (e) {
        let that = this;
        let lsData = that.data;
        let name = e.currentTarget.dataset.name;
        let nameArr = name.split(".");
        let newVal = e.detail.value;
        let nameArrLen = nameArr.length
        for (let i = 0; i <= nameArrLen; i++) {
            let val = lsData[nameArr[i]];
            if (nameArrLen - 1 === i) {
                lsData[nameArr[i]] = newVal;
            } else {
                lsData = lsData[nameArr[i]];
            }
        }
        this.setData({
            submitP: that.data.submitP
        })
        // debugger
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.tapSearch();
    },

    tapSearch(){
        let that = this;
        let keyword = that.data.searchP.keyword;
        that.setData({
            'searchP.keyword': keyword
        });
        that.submitAjax(true);
    },
    search: function(e) {
        let that = this;
        let keyword = that.data.searchP.keyword;
        that.setData({
            'searchP.keyword': keyword
        });
        that.submitAjax(true);
    },
    submitAjax(isInit){
        let that = this;
        let searchP = that.data.searchP;
        if(isInit){
            searchP.p = 1;
        }else{
            // 如果为结束值，不在发送请求
            if(searchP.isOver){
                return false;
            }
        }
        request.post({ 'path': config.memberManage }, searchP).then(function (res) {
            if (res.data.ret == 0) {
                let data = res.data.data;
                let nowList = that.data.list;
                if (data.list.length == 0) { // 数据为空的时候
                  that.setData({
                    haveMemberData: false,
                    noInternet: false
                  })
                    that.autoWindowHeight();
                    return false;
                }else{
                  that.setData({
                    haveMemberData: true,
                    noInternet: false
                  })
                }
                // for (var i = 0; i < data.list.length; i++) {
                //     if (Array.isArray(data.list[i].cars)) {
                //         for (var j = 0; j < data.list[i].cars.length; j++) {
                //             console.log(data.list[i].cars[j], '-----');
                //             data.list[i].carNum = arr.join();
                //         }
                //     }
                // }

                //遍历数据
                if(searchP.p === 1){
                    nowList = data.list;
                }else{
                    nowList.push(...data.list);
                }
                that.setData({
                    list: nowList
                });

                //p的临界点
                if(searchP.p >= data.pagination.total_page){
                    searchP.isOver = true
                }else{
                    searchP.isOver = false
                }
                //p加1
                searchP.p++

                that.setData({
                    'searchP': searchP
                });
                that.autoWindowHeight();
            } else{
              //请求出错的时候
              that.setData({
                haveMemberData: false,
                noInternet: true
              })
            }
        })
    },
    loadMore(){
        this.submitAjax()
    },
    goBackIndex() {
      wx.navigateBack({
        delta: 2
      })
    },
    tapReLoad() {
      var that = this;
      that.submitAjax(true);
    }
})